///	<summary>
///	  This unit contains a class to be used to demonstrate the use of
///	  DAttributes library to validate simple values and store them in ini file.
///	</summary>
unit SimpleConfig;

(*******************************************************************************
  MIT License

  Copyright (c) 2013 - Luthfi Harahap

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE
*******************************************************************************)

interface

uses
  SysUtils
  , Windows
  , DAttributes
  , DAttributes.WinRegistry
  ;

const
  DAYNAMES: array[1..7] of string =
    (
     'Monday'
     , 'Tuesday'
     , 'Wednesday'
     , 'Thursday'
     , 'Friday'
     , 'Saturday'
     , 'Sunday'
    );

type
  [RegKey(HKEY_LOCAL_MACHINE, 'SOFTWARE\MyCompany\MyAppl\2.0\')]
  TMyConfig=class
  private
    FWorkingDir: string;
    FDefaultDay: Integer;
    FDefaultCity: string;
    function GetDefaultCity: string;
    function GetDefaultDay: Integer;
    function GetWorkingDir: string;
    procedure SetDefaultCity(const Value: string);
    procedure SetDefaultDay(const Value: Integer);
    procedure SetWorkingDir(const Value: string);
  public
    constructor Create;

    procedure Reset;

    procedure SaveToRegistry;
    procedure LoadFromRegistry;
  published
    [RegValue('WorkDir', '(no working selected)')]
    property WorkingDir: string read GetWorkingDir write SetWorkingDir;
    [RegValue('DefDayValue', 1)]
    property DefaultDay: Integer read GetDefaultDay write SetDefaultDay;
    [RegValue('DefCityName', 'Jakarta')]
    property DefaultCity: string read GetDefaultCity write SetDefaultCity;
  end;

implementation

uses
  Rtti
  , TypInfo
  , IniFiles
  ;

{ TMyConfig }

constructor TMyConfig.Create;
begin
  Reset;
end;

function TMyConfig.GetDefaultCity: string;
begin
  Result := FDefaultCity;
end;

function TMyConfig.GetDefaultDay: Integer;
begin
  Result := FDefaultDay;
end;

function TMyConfig.GetWorkingDir: string;
begin
  Result := FWorkingDir;
end;

procedure TMyConfig.LoadFromRegistry;
begin
  DAttributes.WinRegistry.Load(Self);
end;

procedure TMyConfig.Reset;
begin
  DAttributes.Reset(Self);
end;

procedure TMyConfig.SaveToRegistry;
begin
  DAttributes.WinRegistry.Save(Self);
end;

procedure TMyConfig.SetDefaultCity(const Value: string);
begin
  FDefaultCity := Value;
end;

procedure TMyConfig.SetDefaultDay(const Value: Integer);
begin
  FDefaultDay := Value;
end;

procedure TMyConfig.SetWorkingDir(const Value: string);
begin
  FWorkingDir := Value;
end;

end.
